﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using DeviceFinder.DAL;
using DeviceFinder.Models;

namespace DeviceFinder.Controllers
{
    public class LocationController : ApiController
    {
        // GET api/location
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/location/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/location
        public string Post([FromBody]LocationModel value, string AccessToken)
        {
            string tokenStr = AccessToken;
            Dictionary<string, TokenModel> tkDict = HttpContext.Current.Application[OAuthManager.name] as Dictionary<string, TokenModel>;
            TokenModel tk = null;
            try
            {
                tk = tkDict[tokenStr];
                if (tk != null && tk.isAccessToken == true)
                {
                    TrackLogDAL tlDAL = new TrackLogDAL();
                    string IP = HttpContext.Current.Request.UserHostAddress;
                    bool rs = tlDAL.InsertLocationToID(tk.DeviceId, value.Lat, value.Long, IP, DateTime.Now);
                    
                    return "1";
                }
                else
                {
                    return "-1";
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        // PUT api/location/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/location/5
        public void Delete(int id)
        {
        }
    }
}
